<template>
  <div class="time-converter">
    <div class="card">
      <div class="card-header">
        <h2>时间戳转换工具</h2>
      </div>

      <!-- 时间戳转日期 -->
      <div class="convert-section">
        <h3>时间戳转换为日期</h3>
        <div class="input-group">
          <div class="timestamp-type">
            <label class="radio-label">
              <input 
                type="radio" 
                v-model="timestampType" 
                value="seconds"
                @change="convertToDate"
              >
              <span>秒级时间戳</span>
            </label>
            <label class="radio-label">
              <input 
                type="radio" 
                v-model="timestampType" 
                value="milliseconds"
                @change="convertToDate"
              >
              <span>毫秒级时间戳</span>
            </label>
          </div>
          <input 
            type="text" 
            v-model="timestamp"
            :placeholder="timestampType === 'seconds' ? '请输入秒级时间戳' : '请输入毫秒级时间戳'"
            class="text-input"
          >
          <button 
            class="convert-btn"
            @click="convertToDate"
          >
            转换为日期
          </button>
        </div>

        <div class="result-section" v-if="dateResult">
          <div class="result-item">
            <span class="label">日期：</span>
            <span class="value">{{ dateResult.date }}</span>
            <button class="copy-btn" @click="copyToClipboard(dateResult.date)">复制</button>
          </div>
          <div class="result-item">
            <span class="label">时间：</span>
            <span class="value">{{ dateResult.time }}</span>
            <button class="copy-btn" @click="copyToClipboard(dateResult.time)">复制</button>
          </div>
          <div class="result-item">
            <span class="label">时区：</span>
            <span class="value">{{ dateResult.timezone }}</span>
            <button class="copy-btn" @click="copyToClipboard(dateResult.timezone)">复制</button>
          </div>
          <div class="result-item">
            <span class="label">GMT：</span>
            <span class="value">{{ dateResult.gmt }}</span>
            <button class="copy-btn" @click="copyToClipboard(dateResult.gmt)">复制</button>
          </div>
        </div>
      </div>

      <!-- 日期转时间戳 -->
      <div class="convert-section">
        <h3>日期转换为时间戳</h3>
        <div class="input-group">
          <input 
            type="date" 
            v-model="date"
            class="date-input"
          >
          <input 
            type="time" 
            v-model="time"
            class="time-input"
          >
          <button 
            class="convert-btn"
            @click="convertToTimestamp"
          >
            转换为时间戳
          </button>
        </div>

        <div class="result-section" v-if="timestampResult">
          <div class="result-item">
            <span class="label">秒：</span>
            <span class="value">{{ timestampResult.seconds }}</span>
            <button class="copy-btn" @click="copyToClipboard(timestampResult.seconds)">复制</button>
          </div>
          <div class="result-item">
            <span class="label">毫秒：</span>
            <span class="value">{{ timestampResult.milliseconds }}</span>
            <button class="copy-btn" @click="copyToClipboard(timestampResult.milliseconds)">复制</button>
          </div>
          <div class="result-item">
            <span class="label">时区：</span>
            <span class="value">{{ timestampResult.timezone }}</span>
            <button class="copy-btn" @click="copyToClipboard(timestampResult.timezone)">复制</button>
          </div>
          <div class="result-item">
            <span class="label">GMT：</span>
            <span class="value">{{ timestampResult.gmt }}</span>
            <button class="copy-btn" @click="copyToClipboard(timestampResult.gmt)">复制</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'

// 输入值
const timestamp = ref('')
const date = ref('')
const time = ref('')
const timestampType = ref('seconds') // 'seconds' 或 'milliseconds'

// 转换结果
const dateResult = ref<{
  date: string
  time: string
  timezone: string
  gmt: string
} | null>(null)

const timestampResult = ref<{
  seconds: number
  milliseconds: number
  timezone: string
  gmt: string
} | null>(null)

// 初始化当前时间
onMounted(() => {
  const now = new Date()
  
  // 设置日期和时间输入框的默认值
  date.value = now.toISOString().split('T')[0]
  time.value = now.toTimeString().slice(0, 5)
  
  // 设置时间戳的默认值
  timestamp.value = Math.floor(now.getTime() / 1000).toString()
  
  // 自动执行转换
  convertToDate()
  convertToTimestamp()
})

// 时间戳转日期
const convertToDate = () => {
  try {
    const ts = parseInt(timestamp.value)
    if (isNaN(ts)) {
      throw new Error('请输入有效的时间戳')
    }

    // 创建日期对象，根据时间戳类型进行转换
    const date = new Date(timestampType.value === 'seconds' ? ts * 1000 : ts)
    if (date.toString() === 'Invalid Date') {
      throw new Error('无效的时间戳')
    }

    // 格式化日期和时间
    const formatDate = date.toLocaleDateString('zh-CN', {
      year: 'numeric',
      month: '2-digit',
      day: '2-digit'
    })
    const formatTime = date.toLocaleTimeString('zh-CN', {
      hour: '2-digit',
      minute: '2-digit',
      second: '2-digit'
    })

    dateResult.value = {
      date: formatDate,
      time: formatTime,
      timezone: 'Asia/Shanghai',
      gmt: date.toString()
    }
  } catch (error) {
    ElMessage.error(error instanceof Error ? error.message : '转换失败')
  }
}

// 日期转时间戳
const convertToTimestamp = () => {
  try {
    if (!date.value || !time.value) {
      throw new Error('请选择日期和时间')
    }

    // 创建日期对象
    const dateTime = new Date(`${date.value}T${time.value}`)
    if (dateTime.toString() === 'Invalid Date') {
      throw new Error('无效的日期或时间')
    }

    const seconds = Math.floor(dateTime.getTime() / 1000)
    const milliseconds = dateTime.getTime()

    timestampResult.value = {
      seconds,
      milliseconds,
      timezone: 'Asia/Shanghai',
      gmt: dateTime.toString()
    }
  } catch (error) {
    ElMessage.error(error instanceof Error ? error.message : '转换失败')
  }
}

// 在 script setup 部分添加复制函数
const copyToClipboard = (text: string | number) => {
  navigator.clipboard.writeText(text.toString())
    .then(() => ElMessage.success('复制成功'))
    .catch(() => ElMessage.error('复制失败'))
}
</script>

<style scoped>
.time-converter {
  padding: 20px;
  max-width: 900px;  /* 增加最大宽度 */
  margin: 0 auto;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  font-size: 16px;  /* 增加基础字体大小 */
}

.card {
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.06);
  padding: 24px;
}

.card-header {
  margin-bottom: 24px;
}

.card-header h2 {
  margin: 0;
  font-size: 24px;  /* 增加标题字体大小 */
  color: #2d3748;
  font-weight: 600;
}

.convert-section {
  margin-bottom: 32px;
}

.convert-section h3 {
  font-size: 20px;  /* 增加子标题字体大小 */
  color: #2d3748;
  margin-bottom: 20px;
  font-weight: 500;
}

.input-group {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.input-group > div:not(.timestamp-type) {
  display: flex;
  gap: 12px;
}

@media (min-width: 640px) {
  .input-group {
    flex-direction: row;
    align-items: center;
  }
}

.text-input,
.date-input,
.time-input {
  padding: 10px 16px;  /* 增加输入框内边距 */
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  font-size: 16px;  /* 增加输入框字体大小 */
  flex: 1;
  transition: all 0.3s ease;
}

.text-input:focus,
.date-input:focus,
.time-input:focus {
  border-color: #4f46e5;
  outline: none;
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
}

.convert-btn {
  padding: 10px 20px;  /* 增加按钮内边距 */
  background: #4f46e5;
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
  font-size: 16px;  /* 增加按钮字体大小 */
  font-weight: 500;
  white-space: nowrap;
}

.convert-btn:hover {
  background: #4338ca;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(79, 70, 229, 0.2);
}

.result-section {
  background: #f8fafc;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  padding: 16px;
}

.result-item {
  display: flex;
  align-items: center;
  margin-bottom: 12px;
  gap: 12px;
}

.result-item:last-child {
  margin-bottom: 0;
}

.result-item .label {
  width: 100px;  /* 增加标签宽度 */
  color: #64748b;
  font-size: 16px;  /* 增加标签字体大小 */
}

.result-item .value {
  color: #1e293b;
  font-size: 16px;  /* 增加值的字体大小 */
  font-weight: 500;
  flex: 1;
}

.timestamp-type {
  display: flex;
  gap: 16px;
  align-items: center;
}

.radio-label {
  display: flex;
  align-items: center;
  gap: 4px;
  cursor: pointer;
  user-select: none;
  font-size: 16px;  /* 增加单选框标签字体大小 */
  color: #475569;
}

.radio-label input[type="radio"] {
  margin: 0;
  cursor: pointer;
}

.radio-label span {
  padding-top: 1px;
}

/* 新增复制按钮样式 */
.copy-btn {
  padding: 6px 12px;
  background: #f1f5f9;
  color: #475569;
  border: 1px solid #e2e8f0;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s ease;
  font-size: 14px;
  white-space: nowrap;
}

.copy-btn:hover {
  background: #e2e8f0;
  transform: translateY(-1px);
}

.copy-btn:active {
  transform: translateY(0);
}

/* 调整响应式布局 */
@media (max-width: 768px) {
  .time-converter {
    font-size: 14px;
  }
  
  .card-header h2 {
    font-size: 20px;
  }
  
  .convert-section h3 {
    font-size: 18px;
  }
  
  .result-item {
    flex-wrap: wrap;
  }
  
  .result-item .label {
    width: 80px;
  }
  
  .copy-btn {
    padding: 4px 8px;
    font-size: 12px;
  }
}
</style> 